<template>
  <div>
    <!-- <MessageIndex type="error" :message="error.message" v-if="error.status"></MessageIndex> -->
    <!-- <h1 v-if="isLoading">加载中~~~~~</h1> -->
    <LoaderIng v-if="isLoading" text="我已经拼命加载了"></LoaderIng>
    <GlobalHeader :user="currentUser"></GlobalHeader>
    <!-- <UploaderFile
      action="/upload"
      :beforeUpload="beforeUpload"
      @file-uploaded="onFileUploaded"
    >
      <h2>点击上传</h2>
      <template #loading>
        <div class="spinner-boder" role="status">
          <span class="sr-only"></span>
        </div>
      </template>
      <template #uploaded="dataProps">
        <img :src="dataProps.uploadedData.data.url" alt="" width="500"/>
      </template>
    </UploaderFile> -->
    <RouterView></RouterView>
    <footer class="text-center py-4 text-secondary bg-light mt-6">
      <small>
        <ul class="list-inline mb-0">
          <li class="list-inline-item">专栏</li>
          <li class="list-inline-item">文档</li>
          <li class="list-inline-item">详情</li>
          <li class="list-inline-item">地址</li>
          <li class="list-inline-item">联系</li>
        </ul>
      </small>
    </footer>
  </div>
</template>
<script lang="ts">
import { defineComponent, computed, onMounted, watch } from "vue";
import GlobalHeader from "./components/GlobalHeader.vue";
import LoaderIng from "./components/LoaderIng.vue";
import { useStore } from "vuex";
import { GolbalDataProps, ResponseType, IamgeProps } from "./store";
import axios from "axios";
import createMessage from "./hooks/createMessage";
import UploaderFile from "./components/UploaderFile.vue";
export default defineComponent({
  name: "App",
  components: {
    GlobalHeader,
    LoaderIng,
  },
  setup() {
    const store = useStore<GolbalDataProps>();
    const currentUser = computed(() => store.state.user);
    const isLoading = computed(() => store.state.loading);
    const token = computed(() => store.state.token);
    const error = computed(() => store.state.error);
    onMounted(() => {
      //isLogin为false头部显示的为登录和注册就算是token保存到本地也没有意义
      //isLogin为true 头部显示的是 你好xxx此时xxx并没有获取到用户信息，前提是已经保存了token
      // if (!currentUser.value.isLogin && token.value) {
      //   axios.defaults.headers.common.Authorization = `Bearer ${token.value}`;
      //   store.dispatch("fetchCurrentUser");
      // }
      watch(
        () => error.value.status,
        () => {
          const { status, message } = error.value;
          if (status && message) {
            createMessage(message, "error");
          }
        }
      );
    });
    // const beforeUpload = (file: File) => {
    //   console.log(file);
    //   const isJPG = file.type === "image/jpeg";
    //   if (!isJPG) {
    //     createMessage("上传图片只能是JPG格式！", "error");
    //   }
    //   return isJPG;
    // };
    // const onFileUploaded = (rawData: ResponseType<IamgeProps>) => {
    //   createMessage(`上传图片id${rawData.data._id}`, "success");
    // };
    return {
      currentUser,
      isLoading,
      error
    };
  },
});
</script>
